package com.grad.why.studentsystem.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.grad.why.studentsystem.domain.Course;
import com.grad.why.studentsystem.domain.CourseArrange;
import com.grad.why.studentsystem.domain.current.CurrentCourseArrangeInfo;
import com.grad.why.studentsystem.mapper.CourseArrangeMapper;
import com.grad.why.studentsystem.mapper.CourseMapper;
import com.grad.why.studentsystem.mapper.OptionparamsMapper;
import com.grad.why.studentsystem.service.ClassesTableService;
import com.grad.why.studentsystem.service.CourseArrangeService;
import com.grad.why.studentsystem.util.Util;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

@Component
@Service(interfaceClass = CourseArrangeService.class)
@Transactional
public class CourseArrangeServiceImpl implements CourseArrangeService{
    @Autowired
    private CourseArrangeMapper courseArrangeMapper;

    @Autowired
    private OptionparamsMapper optionparamsMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<CurrentCourseArrangeInfo> getCourseArrangeInfoList(int offset, int limit, String search) {
        try{
            return courseArrangeMapper.getCourseArrangeInfoList(offset,limit,search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getCourseArrangeInfoListLength(String search) {
        try{
            return courseArrangeMapper.getCourseArrangeInfoListLength(search);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<CurrentCourseArrangeInfo> getCourseArrangeInfoByCid(Integer cid) {
        try{
            return courseArrangeMapper.getCourseArrangeInfoByCid(cid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getMaxId() {
        try{
            return courseArrangeMapper.getMaxId();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addCourseArrangeByEntity(CourseArrange courseArrange) {
        try{
            return courseArrangeMapper.insertSelective(courseArrange);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateCourseArrangeInfoByEntity(CourseArrange courseArrange) {
        try{
            return courseArrangeMapper.updateByPrimaryKeySelective(courseArrange);
        }catch (Exception e){
            e.printStackTrace();
        }
        return -0;
    }

    @Override
    public int deleteCourseArrangeInfosById(int i) {
        try{
            return courseArrangeMapper.deleteByPrimaryKey(i);
        }catch (Exception E){
            E.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addCourseArrangeInfosByExcelFile(String uploadCurrentFileName) {
        try{
            InputStream is = new FileInputStream(uploadCurrentFileName);
            Workbook wb = null;

            if(uploadCurrentFileName.split("\\.")[1].equals("xls")){
                wb = new HSSFWorkbook(is);
            }else if(uploadCurrentFileName.split("\\.")[1].equals("xlsx")){
                wb = new XSSFWorkbook(is);
            }
            int sheetNumber = wb.getNumberOfSheets();
            int InsertCount = 0;

            Row title = wb.getSheetAt(0).getRow(0);
            //修改
            if(!title.getCell(0).getStringCellValue().equals("教室编号")||
                    !title.getCell(1).getStringCellValue().equals("教室名称")||
                    !title.getCell(2).getStringCellValue().equals("节次")||
                    !title.getCell(3).getStringCellValue().equals("课程编号")||
                    !title.getCell(4).getStringCellValue().equals("工作日")){
                return 0;
            }

            for(int i = 0;i<sheetNumber;i++) {
                int index = 1;
                for(Row row : wb.getSheetAt(i)){
                    if(row == null){
                        continue;
                    }else{
                        if(index == 1){

                        }else{
                            if(row.getCell(0) == null){
                                index++;
                                continue;
                            }else{
                                CourseArrange courseArrange = new CourseArrange();
                                courseArrange.setCoursearrangeid(courseArrangeMapper.getMaxId() + 1);
                                courseArrange.setClassroomid((int)Double.parseDouble(Util.formGetCellType(row.getCell(0))));
                                courseArrange.setCourseindex(Util.formGetCellType(row.getCell(2)));
                                int cid = (int)Double.parseDouble(Util.formGetCellType(row.getCell(3)));
                                courseArrange.setCid(cid);
                                courseArrange.setWeekday(Util.formGetCellType(row.getCell(4)));
                                Course course = courseMapper.selectByPrimaryKey(cid);
                                courseArrange.setStartweek(course.getCoursestartweek());
                                courseArrange.setEndweek(course.getCourseendweek());
                                courseArrangeMapper.insertSelective(courseArrange);
                                InsertCount = InsertCount + 1;
                            }
                        }
                        index++;
                    }
                }
            }
            return InsertCount;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
}
